from django.shortcuts import render, HttpResponse

# Create your views here.
# 构造全局空列表，保存参数
num_lyst = []


def info(origin):
    """装饰器函数：在执行视图函数前，完成对输入数值的处理"""

    def inner(request):
        if request.method == 'GET':
            for k, v in request.GET.items():
                param, value_of_param = k, float(v)
                # print("参数>>>", param, "   值", value_of_param)
                # 将参数保存到列表
                num_lyst.append(value_of_param)
        res = origin(request)
        num_lyst.clear()  # 清空全局变量
        return res

    return inner


@info
def add(request):
    """
    加法计算
    :param request:
    :return:res
    """
    res = num_lyst[0] + num_lyst[1]
    return HttpResponse(res)


@info
def multiply(request):
    """
    乘法计算
    :param request:
    :return:
    """
    res = num_lyst[0] * num_lyst[1]
    return HttpResponse(res)


@info
def sub(request):
    """
    减法计算
    :param request:
    :return:
    """
    res = num_lyst[0] - num_lyst[1]
    return HttpResponse(res)


@info
def div(request):
    """
    除法计算
    :param request:
    :return:
    """
    res = num_lyst[0] / num_lyst[1]
    return HttpResponse(res)